// pages/MyCover/sharehetong/sharehetong.js
Page({
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)

    this.clickPup(e.detail.value);
  },
  /**
   * 页面的初始数据
   */
  data: {
    click: false, //是否显示弹窗内容
    option: false, //显示弹窗或关闭弹窗的操作动画
    date: "",
    enddate: "",
    yuedingdate: "",
    jiaimg: "../../../images/home/add.jpg",
    yiimg: "../../../images/home/add.jpg",
    detail: [],
    htpic:""
  },
  nextclick: function () {
    wx: wx.navigateTo({
      url: '../nextview/nextview',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  bingDateChange: function (e) {
    this.setData({

      date: e.detail.value
    })
  },
  imgYu: function (event) {
    var that = this;
    var src = wx.getStorageSync("htPic");//获取data-src
    var imgList = [wx.getStorageSync("htPic")];//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  endDateChange: function (e) {
    this.setData({
      enddate: e.detail.value
    })
  },
  yuedingDateChange: function (e) {
    this.setData({
      yuedingdate: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      htpic: wx.getStorageSync("htPic")

    })
    var aa = [];
    // var jsonStr = options.detail.replace(" ", "");
    // if (typeof jsonStr != 'object') {
    //   jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
    //    aa = JSON.parse(jsonStr)

    // }

    aa = JSON.parse(wx.getStorageSync("detail"));



    var array = new Array();
    for (var i = 0; i < Object.keys(aa).length;i++){
        var keys = Object.keys(aa)[i];//取到“香蕉1”

      var info = {};
      info["all"] = keys;
      info["id"] = keys.split('_')[0];
      info["val"] = keys.split('_')[1];
      array.push(info)

    }


    this.setData({
      detail: array
    })

    wx.reLaunch({

    })
  },
  choosejiaWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        that.setData({
          // tempFilePath可以作为img标签的src属性显示图片
          jiaimg: res.tempFilePaths[0],
        })
      }
    })
  },
  chiosejiaimage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#aaaaaa",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.choosejiaWxImage('album')
          } else if (res.tapIndex == 1) {
            that.choosejiaWxImage('camera')
          }
        }
      }
    })
  },

  // 乙的拍照
  chooseyiWxImage: function (type) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        that.setData({
          // tempFilePath可以作为img标签的src属性显示图片
          yiimg: res.tempFilePaths[0],
        })
      }
    })
  },
  //乙的拍照
  chioseyiimage: function () {
    var that = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#aaaaaa",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseyiWxImage('album')
          } else if (res.tapIndex == 1) {
            that.chooseyiWxImage('camera')
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.showModal({

      title: '提示',

      confirmText: '确定',

      cancelText: '取消',

      content: '页面有数据未保存是否保存',

      success: function (res) {

        if (res.confirm) {

          wx.redirectTo({

            // url: '/pages/index'
          })
        } else if (res.cancel) {
          return;
        }
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  linkclick: function () {

  },
  preview:function(data){
    let _that = this;
    wx.request({
      url: "http://127.0.0.1:8082/wx/order/makenewHtPic",
      data: { orderId: 14 },
      method: "GET",
      header: {
        "Content-Type": "application/json",
        // Cookie: "PHPSESSID=" + wx.getStorageSync("PHPSESSID")
        'X-Litemall-Token': wx.getStorageSync('token')
      },
      success: function (a) {

        _that.setData({
          htpic: a.data.data

        })
      },
      fail: function (a) {
        // t.WxLogin();
      }
    });
  },

  // 用户点击显示弹窗
  clickPup: function (data) {
    // var a = JSON.stingify(data);
    data.type = 1;
    wx.request({
      url: "http://127.0.0.1:8082/wx/order/submit",
      // data: { gId: 1181023, body: data,type:1},
            data: data,
      method: "POST",
      header: {
        "Content-Type": "application/json",
        // Cookie: "PHPSESSID=" + wx.getStorageSync("PHPSESSID")
        'X-Litemall-Token': wx.getStorageSync('token')
      },
      success: function (a) {
        // console.log(a.data), 0 == a.data && t.WxLogin();

        // this.setData({
        //   postdata: a.data.banner
        // })
      },
      fail: function (a) {
        // t.WxLogin();
      }
    });

    let _that = this;
    if (!_that.data.click) {
      _that.setData({
        click: true,
      })
    }

    if (_that.data.option) {
      _that.setData({
        option: false,
      })

      // 关闭显示弹窗动画的内容，不设置的话会出现：点击任何地方都会出现弹窗，就不是指定位置点击出现弹窗了
      setTimeout(() => {
        _that.setData({
          click: false,
        })
      }, 500)


    } else {
      _that.setData({
        option: true
      })
    }
  },

})